package com.lcf.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcf.framework.vo.PageVO;
import com.lcf.system.dao.SysDictDao;
import com.lcf.system.dto.dict.SysDictDTO;
import com.lcf.system.entity.SysDict;
import com.lcf.system.manager.SysDictManager;
import com.lcf.system.query.dict.SysDictPageQuery;
import com.lcf.system.service.SysDictService;
import com.lcf.system.vo.dict.SysDictDetailVO;
import com.lcf.system.vo.dict.SysDictVO;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 字段值(SysDict)表服务实现类
 *
 * @author lcf
 * @since 2025-03-16 22:04:24
 */
@Service("sysDictService")
public class SysDictServiceImpl extends ServiceImpl<SysDictDao, SysDict> implements SysDictService {
    @Resource
    private SysDictManager sysDictManager;

    @Override
    public void add(SysDictDTO dto) {
        sysDictManager.checkExist(null, dto);

        SysDict sysDict = BeanUtil.copyProperties(dto, SysDict.class);
        sysDict.insert();
    }

    @Override
    public void edit(long dictId, SysDictDTO dto) {
        sysDictManager.checkExist(dictId, dto);

        SysDict sysDict = BeanUtil.copyProperties(dto, SysDict.class);
        sysDict.setId(dictId);
        sysDict.updateById();
    }

    @Override
    public void delete(long dictId) {
        sysDictManager.checkId(dictId);
        removeById(dictId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> ids) {
        sysDictManager.checkId(ids);
        removeBatchByIds(ids);
    }

    @Override
    public SysDictDetailVO get(long dictId) {
        SysDict sysDict = getById(dictId);

        return BeanUtil.copyProperties(sysDict, SysDictDetailVO.class);
    }

    @Override
    public PageVO<SysDictVO> page(SysDictPageQuery query) {
        Page<SysDict> page = lambdaQuery()
                .eq(StringUtils.isNotBlank(query.getTypeCode()), SysDict::getTypeCode, query.getTypeCode())
                .page(query.toPage());

        return new PageVO<>(page, SysDictVO.class);
    }
}

